﻿using Vision001.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Vision001.Helper
{
    public static class SysConfigsHelper
    {
        /// <summary>
        /// 读取xml配置文件
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static async Task<bool> ReadConfig()
        {
            try
            {
                //创建配置xml并写入
                if (!File.Exists(Global._ConfigPath))
                {
                    //当配置文件不存在时
                    var data = new ConfigsModel
                    {
                        Configs = new List<Config>()
                        {
                            new Config(){ ExposureTime=2000,ConfigName = "配置1",IsUse = false,ExposureTime2=160000,Ip = "192.168.1.1",Port = 502, Configid = 1, PathMothedaPath = "ddd", IsMes = false, Mesaddress = "127.0.0.1"},
                            new Config(){ExposureTime=2000,ConfigName = "配置2",IsUse = false,ExposureTime2=160000,Ip = "192.168.1.1",Port = 502, Configid = 2, PathMothedaPath = "ddd", IsMes = false, Mesaddress = "127.0.0.1"},
                            new Config(){ExposureTime=2000, ConfigName = "配置3",IsUse = false,ExposureTime2 = 160000, Ip = "192.168.1.1",Port = 502, Configid = 3, PathMothedaPath = "ddd", IsMes = false, Mesaddress = "127.0.0.1"}
                        },
                        Users = new List<User>()
                        {
                            new User(){UserName = "admin",UserPassword = "3",UserLevel = 3},
                            new User(){UserName = "操作员",UserPassword = "1",UserLevel = 1},
                            new User(){UserName = "工程师",UserPassword = "2",UserLevel = 2}
                        }
                    };

                    // 创建 XmlSerializer
                    XmlSerializer serializer = new XmlSerializer(typeof(ConfigsModel));
                    // 创建文件流并序列化
                    using (FileStream fs = new FileStream(Global._ConfigPath, FileMode.Create))
                    {
                        serializer.Serialize(fs, data);
                    }

                    //找不到配置文件,返回false，并生成默认配置，然后初始化失败
                    return false;
                }
                else
                {
                    ConfigsModel deserializedPerson;
                    //读取配置文件
                    // 从XML反序列化为对象
                    XmlSerializer serializer = new XmlSerializer(typeof(ConfigsModel));
                    using (var reader = new StreamReader(Global._ConfigPath))
                    {
                        deserializedPerson = (ConfigsModel)serializer.Deserialize(reader);
                    }
                    //存放所有的配置和用户信息
                    Global._Configs = deserializedPerson.Configs;
                    Global._Users = deserializedPerson.Users;
                    //加载当前选择的配置
                    var useConfig = Global._Configs.Where(g => g.IsUse == true).ToList();
                    if (useConfig.Count > 0)
                    {
                        Global._Config = useConfig[0].Clone() as Config;
                    }
                    else
                    {
                        throw new Exception("配置文件没有可启动项！！");
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }

            return true;
        }
    }
}